/* QtLua -- Lua bindings for Qt
   Copyright (C) 2011, Jarek Pelczar

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA

*/

#include "qluascriptdebuggercodewidget_p.h"
#include "qluascriptprogram.h"
#include "qluascriptdebuggerscripteditor_p.h"
#include <QDebug>

qint64 QLuaScriptDebuggerCodeWidget::getScriptId(QLuaScriptDebuggerScriptEditorInterface * v) const
{
    if(!v)
        return -1;
    return m_views.key(v);
}

QLuaScriptDebuggerCodeWidget::QLuaScriptDebuggerCodeWidget(QWidget *parent) :
    QLuaScriptDebuggerCodeWidgetInterface(parent),
    m_scriptModel(0),
    m_breakpointModel(0)
{
    QVBoxLayout * l = new QVBoxLayout(this);
    l->setMargin(0);
    m_stackedWidget = new QStackedWidget(this);
    l->addWidget(m_stackedWidget);
}

QLuaScriptsDebuggerScriptModel * QLuaScriptDebuggerCodeWidget::scriptsModel() const
{
    return m_scriptModel;
}

void QLuaScriptDebuggerCodeWidget::setScriptsModel(QLuaScriptsDebuggerScriptModel * model)
{
    m_scriptModel = model;
}

QLuaScriptBreakpointModel * QLuaScriptDebuggerCodeWidget::breakpointsModel() const
{
    return m_breakpointModel;
}

void QLuaScriptDebuggerCodeWidget::setBreakpointsModel(QLuaScriptBreakpointModel * model)
{
    m_breakpointModel = model;
}

qint64 QLuaScriptDebuggerCodeWidget::currentScriptId() const
{
    return getScriptId(currentView());
}

void QLuaScriptDebuggerCodeWidget::setCurrentScript(qint64 id)
{
#ifdef QT_DEBUG
    qDebug() << this << "Set current script" << id;
#endif
    if(id == -1)
        return;
    QLuaScriptDebuggerScriptEditorInterface * editor = m_views.value(id);
    if(!editor) {
        QLuaScriptProgram program = m_scriptModel->program(id);
        if(program.isNull()) {
#ifdef QT_DEBUG
            qWarning() << this << "Program not found";
#endif
            return;
        }
        editor = new QLuaScriptDebuggerScriptEditor(m_stackedWidget);
        editor->setText(program.text());
        m_stackedWidget->addWidget(editor);
        m_views.insert(id, editor);
    }
    m_stackedWidget->setCurrentWidget(editor);
}

QLuaScriptDebuggerScriptEditorInterface * QLuaScriptDebuggerCodeWidget::currentView() const
{
    return qobject_cast<QLuaScriptDebuggerScriptEditorInterface *>(m_stackedWidget->currentWidget());
}
